package net.scalan.jpa.model;

import java.util.List;
import net.scalan.jpa.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import net.scalan.jpa.exceptions.NonexistentEntityException;

/**
 *
 * @author scalan
 */
public class DBSchemObjectController {

    public DBSchemObjectController() {
    }

    public void create(DBSchemObject data) {
        PersistenceService ps = PersistenceService.getInstance();
        try {
            ps.beginTx();
            ps.getEntityManager().persist(data);
            ps.commitTx();
        } finally {
            ps.close();
        }
    }

    public void update(DBSchemObject data) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = PersistenceService.getInstance().getEntityManager();
            em.getTransaction().begin();
            data = em.merge(data);
            em.getTransaction().commit();
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (em != null && em.isOpen()) {
                em.close();
            }
        }
    }

    public void delete(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = PersistenceService.getInstance().getEntityManager();
            em.getTransaction().begin();
            DBSchemObject data;
            try {
                data = em.getReference(DBSchemObject.class, id);
                data.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The DBTest with id " + id + " no longer exists.", enfe);
            }
            em.remove(data);
            em.getTransaction().commit();
        } finally {
            if (em != null && em.isOpen()) {
                em.close();
            }
        }
    }

    public DBSchemObject getObject(Long id) {
        EntityManager em = PersistenceService.getInstance().getEntityManager();
        try {
            return em.find(DBSchemObject.class, id);
        } finally {
            if (em != null && em.isOpen()) {
                em.close();
            }
        }
    }

    public List<DBSchemObject> getObjects(Long id) {
        System.out.println("!111111");
        PersistenceService ps = PersistenceService.getInstance();
        System.out.println("!1111112222222");
        try {
        System.out.println("!111111333333333");
            Query q = ps.getEntityManager().createQuery(
                    "SELECT d "
                    + "FROM DBSchemObject AS d "
                    + //"WHERE d.firm=:firm " +
                    "ORDER BY d.id");
            //q.setParameter("firm", firm);
        System.out.println("!1111114444444444");
            return q.getResultList();
        } finally {
        System.out.println("!11111155555555555");
            ps.close();
        System.out.println("!11111166666666666");
        }
    }
}
